<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/common.css">
</head>
<body>
    <!-- 导航栏 -->
    <div class="nav">
        <!-- 左边 -->
        <div class="nav-left">
            <img src="img/logo2.jpg" alt="">
            <span>我的博客系统</span>
        </div>
        <!-- 右边 -->
        <div class="nav-right">
            <a href="blog_list.html">主页</a>
            <a href="blog_edit.html">写博客</a>
            <a href="api/user/logout">注销</a>
        </div>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左边：用户信息 -->
        <div class="container-left">
            <div class="card">
                <!-- 头像 -->
                <img src="img/doge.jpg">
                <!-- 用户名称 -->
                <h3>薛先生</h3>
                <a href="www.github.com">github地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右边：博客列表 -->
        <div class="container-right">
            <!-- 一篇文章 -->
            <div class="row">
                <div class="title">遍历的每一篇文章数据的标题</div>
                <div class="date">日期</div>
                <div class="desc">描述</div>
                <div class="to-detail">
                    <a href="blog_content.html">查看全文 >></a>
                </div>
            </div>
        </div>
    </div>
</body>
<script src="js/util.js"></script>
<script>
    //客户端展示页面时，就需要同时展示文章列表的数据
    //加载完页面，就发送ajax请求，获取文章列表的数据
    //返回响应执行回调时：解析响应的文章列表数据，
    //遍历每一篇文章：在<div class="container-right">下，生成一篇文章静态html相同的dom结构
    ajax({
        method: "get",
        url: "api/article/list",
        callback: function(status, responseText){
            // 先判断一下状态码
            if(status == 200){//http响应成功
                //解析响应数据：{"ok": true, data: {"nickname": "", "articles": []}}
                let json = JSON.parse(responseText);
                if(json.success){//操作成功
                    let data = json.data;
                    let nickname = data.nickname;
                    //设置用户昵称
                    let h3 = document.querySelector(".card>h3");
                    h3.innerHTML = nickname;
                    let articles = data.articles;
                    console.log(articles);
                    //解析文章列表的数据，动态的生成html内容，并设置到container-right的标签内容
                    let div = document.querySelector(".container-right");
                    //小心，如果str不赋值就是=undefined，再去拼接字符串，就是unfined....
                    let str = "";
                    for(let a of articles){
                        str +=  `<div class="row">`;
                        str +=      `<div class="title">`;
                        str +=          a.title;
                        str +=      `</div>`;
                        str +=      `<div class="date">`;
                        str +=          a.createTime;
                        str +=      `</div>`;
                        str +=      `<div class="desc">`;
                        str +=          a.content;
                        str +=      `</div>`;
                        //查询一篇文章详情，是需要获取到文章数据：根据id来获取
                        str +=      `<div class="to-detail">`;
                        str +=          `<a href="blog_content.html?id=` + a.id + `">查看全文 >></a>`;
                        str +=      `</div>`;
                        str +=  `</div>`;
                    }
                    div.innerHTML = str;
                }else{
                    alert('错误码['+json.code + '],错误信息：'+ json.message)
                }
            }else{
                alert("响应状态码："+status+"\nbody: "+responseText)
            }
        }
    });
</script>
</html>